package com.wty.cmims.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wty.cmims.pojo.Code;
import com.wty.cmims.pojo.Opinion;
import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.pojo.Schedule;
import com.wty.cmims.service.OpinionService;
import com.wty.cmims.mapper.OpinionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author wty
* @description 针对表【opinion】的数据库操作Service实现
* @createDate 2025-03-10 14:56:42
*/
@Service
public class OpinionServiceImpl extends ServiceImpl<OpinionMapper, Opinion>
    implements OpinionService{
    @Autowired
    private OpinionMapper opinionMapper;

    @Override
    public ResponseResult<Map<String, Object>> opinionFind(Map<String, Object> paramsMap) {
        // 分页
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        Integer pageSize = (Integer) paramsMap.get("pageSize");
        paramsMap.put("start", ((Integer) paramsMap.get("currentPage") - 1) * pageSize);
        responseResult.setPageSize(pageSize);

        List<Map<String, Object>> opinions = new ArrayList<>();
        Integer total = 0;
        Integer role = (Integer) paramsMap.get("role");
        SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            if (paramsMap.get("date") != null){
                date = sdfDay.parse(paramsMap.get("date").toString());
            }
        } catch (ParseException e) {
            System.out.println("日期转换错误");
        }
        if (date != null) {
            paramsMap.put("date", sdfDay.format(date));
        }
        opinions = opinionMapper.findOpinions(paramsMap);
        total = opinionMapper.findOpinionsNum(paramsMap);
        if (opinions != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询反馈列表成功");
            responseResult.setDatas(opinions);
            responseResult.setTotal(total);
            responseResult.setCurrentPage((Integer) paramsMap.get("currentPage"));
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询反馈列表失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> opinionUpdate(Map<String, Object> paramsMap) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        LambdaUpdateWrapper<Opinion> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Opinion::getId, paramsMap.get("id"));
        updateWrapper.set(Opinion::getStatus, paramsMap.get("statusParam"));
        boolean update = this.update(updateWrapper);
        if (update){
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("更改反馈处理状态成功");
        }else{
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("更改反馈处理状态失败");
        }
        return responseResult;
    }
}




